/**
 * org.assess.oa.system.service.impl.TbPostServiceImpl
 */
package org.assess.oa.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.assess.core.common.GeneralUtils;
import org.assess.core.service.BaseServiceImpl;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbPost;
import org.assess.oa.system.bean.TbPostRole;
import org.assess.oa.system.dao.TbPostDAO;
import org.assess.oa.system.dao.TbPostRoleDAO;
import org.assess.oa.system.dao.TbUserPostDAO;
import org.assess.oa.system.service.TbPostService;

/**
 * @description 岗位管理的Service层实现类
 * @author XiaLongxiang
 * @createtime 2012-6-5 下午2:23:17
 */
public class TbPostServiceImpl extends BaseServiceImpl implements TbPostService {
    /** 操作岗位信息表的DAO层对象 */
    private TbPostDAO tbPostDAO;
    
    /** 操作岗位角色关联信息表的DAO层对象 */
    private TbPostRoleDAO tbPostRoleDAO;
    
    /** 操作用户岗位关联信息表的DAO层对象 */
    private TbUserPostDAO tbUserPostDAO;
    
    /** 日志对象 */
    private static final Logger logger = Logger.getLogger(TbPostServiceImpl.class);
    
    public TbPost queryTbPostById(String post_id) {
        TbPost result = null;
        try {
            result = this.tbPostDAO.queryTbPostById(post_id);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String queryTbPostAsList(int page, int rows, TbPost post) {
        String result = "";
        try {
            Map<String, Object> parameters = new HashMap<String, Object>();
            if (post != null) {
                parameters.put("department_code", post.getDepartment_code());
                parameters.put("department_name", post.getDepartment_name());
            }
            
            int totalrecords = this.tbPostDAO.queryTbPostAsInt(parameters);
            List<TbPost> records = this.tbPostDAO.queryTbPostAsList((page - 1) * rows, rows, parameters);
            result = super.toListPageString(page, rows, totalrecords, "getPost_id", records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public List<TbPostRole> queryTbPostRoleByPostIdAsList(TbPost post, boolean include) {
        List<TbPostRole> results = null;
        try {
            if (include) {
                results = this.tbPostRoleDAO.queryTbPostRoleByPostIdAsList(post != null ? post.getPost_id() : "");
            }
        }
        catch (Exception ex) {
            results = new ArrayList<TbPostRole>(0);
            logger.error(ex);
        }
        return results;
    }
    
    public ServiceResult saveTbPost(TbPost post, String roleString, boolean edit) throws Exception {
        ServiceResult result = ServiceResult.error_unknown;
        try {
            if (edit) {
                post.setModify_time(new Date());
                post.setModify_user(super.getLoginUserCode());
                this.tbPostDAO.update(post);
            }
            else {
                post.setPost_id(GeneralUtils.getRandomId(32));
                post.setCreate_time(new Date());
                post.setCreate_user(super.getLoginUserCode());
                this.tbPostDAO.insert(post);
            }
            
            // 接下来保存岗位角色关联信息
            this.tbPostRoleDAO.deleteTbPostRoleByPostId(post.getPost_id());
            String[] role_ids = roleString != null ? roleString.split(",") : null;
            if (role_ids != null && role_ids.length != 0) {
                for (String id : role_ids) {
                    TbPostRole pr = new TbPostRole();
                    pr.setPost_role_id(GeneralUtils.getRandomId(32));
                    pr.setPost_id(post.getPost_id());
                    pr.setRole_id(id.trim());
                    this.tbPostRoleDAO.insert(pr);
                }
            }
            result = ServiceResult.save_success;
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public String deleteTbPost(TbPost post) throws Exception {
        String result = "";
        try {
            String[] post_ids = post.getPost_id().split(",");
            for (String id : post_ids) {
                this.tbPostDAO.deleteTbPostById(id);
                this.tbPostRoleDAO.deleteTbPostRoleByPostId(id);
                this.tbUserPostDAO.deleteTbUserPostByPostId(id);
            }
            result = super.toResultString(true);
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public void setTbPostDAO(TbPostDAO tbPostDAO) {
        this.tbPostDAO = tbPostDAO;
    }
    
    public void setTbPostRoleDAO(TbPostRoleDAO tbPostRoleDAO) {
        this.tbPostRoleDAO = tbPostRoleDAO;
    }
    
    public void setTbUserPostDAO(TbUserPostDAO tbUserPostDAO) {
        this.tbUserPostDAO = tbUserPostDAO;
    }
}
